﻿using DadayunApi.Model;
using KhtApi.DAL;
using KhtApi.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KhtApi.BLL
{
    public class ReportBLL
    {
        ReportDAL dal = null;
        public ReportBLL(AppAuthBase authBase, string baseUri)
        {
            dal = new ReportDAL(authBase, baseUri);
        }
        /// <summary>
        /// 查询某物料编码的库存数量
        /// </summary>
        /// <param name="itemcode">指定的物料编码</param>
        /// <param name="warehouse">指定的仓库名称，如果为空会返回所有仓库的库存数总和</param>
        /// <returns></returns>
        public double GetStock(string itemcode,string warehouse=null)
        {
            List<ReportFilter> filter = new List<ReportFilter>
            {
                new ReportFilter
                {
                    Name="产品编码",
                    Operator="=",
                    DestValue=itemcode
                }
            };
            if (!string.IsNullOrEmpty(warehouse))
            {
                filter.Add(new ReportFilter
                {
                    Name = "仓库",
                    Operator = "=",
                    DestValue = warehouse
                });
            }
            var list = dal.GetStockBalances(filter);
            double qty = 0;
            foreach (var i in list)
            {
                qty += i.Qty;
            }
            return qty;
        }
        /// <summary>
        /// 查询产品BOM
        /// </summary>
        /// <param name="itemCode">产品代码</param>
        /// <param name="version">BOM版本，如果没有输入版本，则返回全部版本</param>
        /// <returns>返回产品的BOM清单。</returns>
        public List<BomReportTpl> GetBoms(string itemCode, double?version=null)
        {
            List<ReportFilter> filter = new List<ReportFilter>
            {
                new ReportFilter
                {
                    Name="产品编码",
                    Operator="=",
                    DestValue=itemCode
                }
            };
            if(version!=null)
            {
                filter.Add(new ReportFilter
                {
                    Name = "版本号",
                    Operator = "=",
                    DestValue = version.ToString()
                });
            }
            List<BomReportTpl> list = dal.GetBomReport(filter);
            return list;
        }
        /// <summary>
        /// 查询销售看板
        /// </summary>
        /// <param name="start"></param>
        /// <param name="to"></param>
        /// <param name="excludePlaned"></param>
        /// <returns></returns>
        public List<SoBillBoard> GetSoBillBoards(DateTime start, DateTime to,bool excludePlaned=false)
        {
            List<ReportFilter> filter = new List<ReportFilter>
            {
                new ReportFilter
                {
                    Name="订单日期",
                    Operator="range",
                    StartValue=start.ToShortDateString(),
                    EndValue=to.ToShortDateString()
                }
            };
            List<SoBillBoard> list = dal.GetSoBillBoard(filter);
            if(excludePlaned)
            {
                List<SoBillBoard> sel = new List<SoBillBoard>();
                foreach (var s in list)
                {
                    double plan = s.PlanQty + s.OldPlanQty;
                    if(plan<s.SoQty)
                    {
                        sel.Add(s);
                    }
                }
                return sel;
            }
            else
            {
                return list;
            }
        }

        internal BomPeggingReport BomPegging(ReportFilter[] filter)
        {
            return dal.BomPegging(filter);
        }

        /// <summary>
        /// 获取锁定数量，尚未提供功能，当前直接返回0
        /// </summary>
        /// <param name="itemCode"></param>
        /// <returns></returns>
        internal double GetLockedQty(string itemCode)
        {
            return 0;
        }
        /// <summary>
        /// 获取在途数量，尚未提供功能，当前直接返回0
        /// </summary>
        /// <param name="itemCode"></param>
        /// <returns></returns>
        internal double GetTransitQty(string itemCode)
        {
            return 0;
        }

        /// <summary>
        /// 查询销售看板
        /// </summary>
        /// <param name="soNo"></param>
        /// <param name="itemCode"></param>
        /// <returns></returns>
        public List<SoBillBoard> GetSoBillBoards(string soNo,string itemCode)
        {
            List<ReportFilter> filter = new List<ReportFilter>
            {
                new ReportFilter
                {
                    Name="销售订单号",
                    Operator="=",
                    DestValue=soNo
                },
                 new ReportFilter
                {
                    Name="产品编码",
                    Operator="=",
                    DestValue=itemCode
                }
            };
            return dal.GetSoBillBoard(filter);
        }
        /// <summary>
        /// 查询产品信息
        /// </summary>
        /// <param name="itemcode"></param>
        /// <returns></returns>
        public ProductInfo GetProductInfo(string itemcode)
        {
            if (string.IsNullOrEmpty(itemcode))
                return null;
            List<ReportFilter> filter = new List<ReportFilter>
            {
                new ReportFilter
                {
                    Name="产品编码",
                    Operator="=",
                    DestValue=itemcode
                }
            };
            List<ProductRepTpl> list = dal.GetProductReport(filter);
            ProductRepTpl product = list.FirstOrDefault(p => p.ItemCode == itemcode);
            return product.ToProductInfo();
        }
    }
}
